package com.mes.schedule.dao;

import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.common.dao.BasicDao;
import com.mes.common.exception.db.BaseDBException;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.mes.schedule.dao.iface.ISOperationTaskDao;
import com.mes.schedule.domain.SOperationTask;

@Repository
public class SOperationTaskDao extends BasicDao<SOperationTask> implements ISOperationTaskDao{
	private static final Log log = LogFactory.getLog(SOperationTaskDao.class);
	
	/**
	 * @see the method use for ScheduleManager
	 * @param scheduleUid
	 * @throws BaseDBException
	 */
	public void deleteAllOperationTask(String scheduleUid) throws BaseDBException {
		try {

			Session session = this.getSession();
			Query query=session.createQuery("delete SOperationTask t where t.scheduleUid=?");
			query.setString(0, scheduleUid);
			query.executeUpdate();

		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ;
	}
	
	/**
	 * @see the method use for STaskDao
	 * @see 通过scheduleUid来获取scheduleoperationtask的实体，同时将taskstate和TASKSCHEDULESTATE的判断条件写死进去
	 * @param scheduleUid
	 * @param taskUid
	 * @return
	 * @throws BaseDBException
	 */
	public List<SOperationTask> getScheduleOperationTask(String scheduleUid) throws BaseDBException {

		List<SOperationTask> sOperationTaskList = new ArrayList<SOperationTask>();
		try {
			
			Session session = this.getSession();
			Criteria crit = session.createCriteria(SOperationTask.class);
			//限制scheduleUid的取值
			crit.add(Restrictions.eq("scheduleUid", scheduleUid));
			
			//约束TASKSCHEDULESTATE不等于0
			crit.add(Restrictions.not(Restrictions.eq("taskScheduleState", 0)));
			
			//约束taskstate<209或者可以为空	
			crit.add(Restrictions.or(Restrictions.isNull("taskState"), 
						Restrictions.lt("taskState",SOperationTask.TASKSTATE_COMPLETED)));//209对应的静态常量
			//crit.add(Restrictions.lt("coTaskState",209));
			
			crit.addOrder(Property.forName("parentTaskUid").asc());
			crit.addOrder(Property.forName("operationId").asc());
		
			
			/*//然后对其进行排序"order by t.parentuid, t.operation_id"
			crit.addOrder(Property.forName("priority").asc());
			crit.addOrder(Property.forName("plangrade").desc());
			crit.addOrder(Property.forName("lateFinish").asc());*/
			sOperationTaskList = crit.list();
		} catch (DataAccessResourceFailureException e) {
			e.printStackTrace();
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} 
		return sOperationTaskList;
	}
  
		/**
		 * @see the method use for ScheduleOperationTaskManager
		 * @param scheduleUid
		 * @param taskUid
		 * @return
		 * @throws BaseDBException
		 */
		public SOperationTask getScheduleOperationTask(String scheduleUid,
				String taskUid) throws BaseDBException {

			SOperationTask operationTask = new SOperationTask();
			try {
				Session session = this.getSession();
				Criteria crit = session.createCriteria(SOperationTask.class);
				crit.add(Restrictions.eq("scheduleUid", scheduleUid));
				crit.add(Restrictions.eq("taskUid", taskUid));		
				operationTask = (SOperationTask) crit.uniqueResult();
			} catch (DataAccessResourceFailureException e) {
				e.printStackTrace();
			} catch (HibernateException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
			return operationTask;
		}
	
}
